package com.yunkaweilai.android.utils.print.data;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;

import com.yunkaweilai.android.model.print.ReceivablesPrintModel;
import com.yunkaweilai.android.model.print.TemplateInfoModel;
import com.yunkaweilai.android.utils.Utils;
import com.yunkaweilai.android.utils.print.PrintDataMaker;
import com.yunkaweilai.android.utils.print.PrinterWriter;
import com.yunkaweilai.android.utils.print.PrinterWriter58mm;
import com.yunkaweilai.android.utils.print.PrinterWriter80mm;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 消费数据生成
 * Created by Alex on 2016/11/10.
 */

public class ReceivablesPrintMaker implements PrintDataMaker {

    private Context context;
    private int width;
    private int height;
    private TemplateInfoModel templateInfoModel;
    private ReceivablesPrintModel receivablesPrintModel;
    private File imageFile;

    public ReceivablesPrintMaker(Context context, int width, int height, TemplateInfoModel templateInfoModel, ReceivablesPrintModel receivablesPrintModel, File imageFile) {
        this.context = context;
        this.width = width;
        this.height = height;
        this.templateInfoModel = templateInfoModel;
        this.receivablesPrintModel = receivablesPrintModel;
        this.imageFile = imageFile;
    }

    @Override
    public List<byte[]> getPrintData(int type) {
        TemplateInfoModel.DataBean.InfosBean info = templateInfoModel.getData().getInfo();
        ReceivablesPrintModel.DataBean.OrderInfoBean order_info = receivablesPrintModel.getData().getOrder_info();
        ArrayList<byte[]> data = new ArrayList<>();
        try {
                final PrinterWriter printer;
                printer = type == PrinterWriter58mm.TYPE_58 ? new PrinterWriter58mm(height, width) : new PrinterWriter80mm(height, width);
                printer.printLineFeed();
                printer.setLineHeight(8);
                //编号
                if ("1".equals(info.getIs_num())) {
                    printer.setAlignRight();
                    printer.setFontSize(0);
                    printer.print("编号: " + order_info.getOrder_num());
                    printer.printLineFeed();
                    printer.printLineFeed();
                }

                //标题1
                if ("1".equals(info.getIs_store_name())) {
                    printer.setAlignCenter();
                    printer.setFontSize(1);
                    printer.setEmphasizedOn();
                    printer.print(info.getStore_name());
                    printer.setFontSize(0);
                    printer.setEmphasizedOff();
                    printer.setAlignLeft();
                    printer.printLineFeed();
                    printer.printLineFeed();
                }
                //标题2
                if ("1".equals(info.getIs_receipt_name())) {
                    printer.setAlignCenter();
                    printer.print(info.getReceipt_name());
                    printer.printLineFeed();
                    printer.setAlignLeft();
                    printer.printLineFeed();
                }
                //打单时间
                if ("1".equals(info.getIs_create_time())) {
                    printer.print("交易时间:" + order_info.getConsume_time());
                    printer.printLineFeed();
                    printer.printLineFeed();
                }
                //单据号
                if ("1".equals(info.getIs_tid())) {
                    printer.print("单据号:" + order_info.getTid());
                    printer.printLineFeed();
                    printer.printLineFeed();
                }
                //操作员
                if ("1".equals(info.getIs_create_name())) {
                    printer.print("操作员:" + order_info.getCreate_name());
                    printer.printLineFeed();
                    printer.printLineFeed();
                }

                //提成员工
                if ("1".equals(info.getIs_employee())) {
                    printer.print("提成员工:" + order_info.getCommissionName());
                    printer.printLineFeed();
                    printer.printLineFeed();
                }

                if (receivablesPrintModel.getData().getMember_info() == null || receivablesPrintModel.getData().getMember_info().getCard_no() == null) {

                } else {
                    //会员卡号
                    if ("1".equals(info.getIs_card_no())) {
                        printer.print("会员卡号:" + order_info.getCard_no());
                        printer.printLineFeed();
                        printer.printLineFeed();
                    }
                    //会员姓名
                    if ("1".equals(info.getIs_member_name())) {
                        printer.print("会员姓名:" + order_info.getMember_name());
                        printer.printLineFeed();
                        printer.printLineFeed();
                    }
                }

                if (order_info.getGoodsList().size() > 0) {
                    printer.printLine();
                    printer.printLineFeed();
                    //商品明细
                    if ("1".equals(info.getIs_goods_list())) {
                        printer.printInOneLine("商品", "单价", "数量", "小计", 0);
                        printer.printLineFeed();
                        printer.printLine();
                        printer.printLineFeed();
                        List<ReceivablesPrintModel.DataBean.OrderInfoBean.GoodsListBean> goodsList = order_info.getGoodsList();
                        for (int i = 0; i < goodsList.size(); i++) {
                            ReceivablesPrintModel.DataBean.OrderInfoBean.GoodsListBean goodsListBean = goodsList.get(i);
//                        if ("PrintDicountPrice".equals(goodsListBean.getPrint_price_type())){
                            if ("PrintShopPrice".equals(goodsListBean.getPrint_price_type())) {
                                printer.printInOneLine(goodsListBean.getGoods_name(), goodsListBean.getShop_price(),
                                        goodsListBean.getGoods_num(),
                                        Utils.ManageMoney((Float.parseFloat(goodsListBean.getShop_price()) * Float.parseFloat(goodsListBean.getGoods_num())) + "")
                                        , 0);
                            } else {
                                printer.printInOneLine(goodsListBean.getGoods_name(), goodsListBean.getShop_discount_price(),
                                        goodsListBean.getGoods_num(),
                                        Utils.ManageMoney((Float.parseFloat(goodsListBean.getShop_discount_price()) * Float.parseFloat(goodsListBean.getGoods_num())) + "")
                                        , 0);
                            }

                            printer.printLineFeed();
                            printer.printLine();
                            printer.printLineFeed();
                        }
                    }
                }

                //原价
                if ("1".equals(info.getIs_goods_prcie())) {
                    printer.print("原价:" + order_info.getZ_goods_discount_price());
                    printer.printLineFeed();
                    printer.printLineFeed();
                }

                //优惠
                if ("1".equals(info.getIs_discount_price())) {
                    printer.print("优惠:" + Utils.ManageMoney((Float.parseFloat(order_info.getCoupon_price()) + Float.parseFloat(order_info.getDiscount_price())) + ""));
                    printer.printLineFeed();
                    printer.printLineFeed();
                }

                //应收金额
                if ("1".equals(info.getIs_payment())) {
                    printer.print("应收金额:" + order_info.getPayment());
                    printer.printLineFeed();
                    printer.printLineFeed();
                }

                //支付明细
                if ("1".equals(info.getIs_pay_info())) {
                    if ("现金支付".equals(order_info.getPayment_type())) {
                        printer.print(order_info.getPayment_type() + ":" + order_info.getCash_price());
                        printer.printLineFeed();
                        printer.printLineFeed();
                    } else {
                        printer.print(order_info.getPayment_type() + ":" + order_info.getPayment());
                        printer.printLineFeed();
                        printer.printLineFeed();
                    }
                }

                //卡内剩余
                if ("1".equals(info.getIs_member_goods_list())) {
                    if (Utils.isEmpty(receivablesPrintModel.getData().getMember_info().getMember_jici())) {
                        printer.print("卡内剩余:");
                    } else {
                        printer.print("卡内剩余:" + receivablesPrintModel.getData().getMember_info().getMember_jici());
                    }
                    printer.printLineFeed();
                    printer.printLineFeed();
                }
                //赠送积分
                if ("1".equals(info.getIs_points())) {
                    printer.print("赠送积分:" + order_info.getPoint());
                    printer.printLineFeed();
                    printer.printLineFeed();
                }

                if (receivablesPrintModel.getData().getMember_info() == null || receivablesPrintModel.getData().getMember_info().getMember_points() == null) {

                } else {
                    //剩余积分
                    if ("1".equals(info.getIs_member_points())) {
                        printer.print("剩余积分:" + receivablesPrintModel.getData().getMember_info().getMember_points());
                        printer.printLineFeed();
                        printer.printLineFeed();
                    }
                    //余额
                    if ("1".equals(info.getIs_member_balance())) {
                        printer.print("余额:" + receivablesPrintModel.getData().getMember_info().getMember_price());
                        printer.printLineFeed();
                        printer.printLineFeed();
                    }
                }

                printer.printLine();
                printer.printLineFeed();
                //备注
                if ("1".equals(info.getIs_remarks())) {
                    printer.print("备注:" + order_info.getRemark());
                    printer.printLineFeed();
                    printer.printLineFeed();
                }


                //脚注1
                if ("1".equals(info.getIs_footnote_one())) {
                    printer.setAlignCenter();
                    printer.print(info.getFootnote_one() + "");
                    printer.printLineFeed();
                    printer.printLineFeed();
                    printer.setAlignLeft();
                }
                //脚注2
                if ("1".equals(info.getIs_footnote_two())) {
                    printer.setAlignCenter();
                    printer.print(info.getFootnote_two() + "");
                    printer.printLineFeed();
                    printer.printLineFeed();
                    printer.setAlignLeft();
                }

                //联系电话
                if ("1".equals(info.getIs_store_tel())) {
                    printer.print("联系电话:" + info.getReceipt_phone());
                    printer.printLineFeed();
                    printer.printLineFeed();
                }
                //店铺地址
                if ("1".equals(info.getIs_store_address())) {
                    printer.print("店铺地址:" + info.getReceipt_address());
                    printer.printLineFeed();
                    printer.printLineFeed();
                }

                if ("1".equals(info.getIs_receipt_image())) {
                    if (imageFile != null) {
                        printer.setAlignCenter();
                        Bitmap bitmap = printer.scalingBitmap(BitmapFactory.decodeFile(imageFile.getPath()), 50);
                        Matrix matrix = new Matrix();
                        matrix.postScale(0.5f, 0.5f); //长和宽放大缩小的比例
                        Bitmap resizeBmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
                        printer.printBitmap(resizeBmp);
                        printer.setAlignLeft();
                    }
                }
                printer.printLineFeed();
                printer.printLineFeed();
                printer.feedPaperCutPartial();
                data.add(printer.getDataAndClose());
            return data;
        } catch (Exception e) {
            return new ArrayList<>();
        }
    }



}
